package com.hd.crdao.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.google.gson.Gson;
import com.hd.crdao.R;
import com.hd.crdao.base.BaseSecondActivity;
import com.hd.crdao.bean.MsgBean;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.ALog;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.utils.SPUtils;
import com.hd.crdao.view.ExpandHeadView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import okhttp3.Call;

/**
 *
 *@author Programming
 *@date 2017/7/21 0021
 */

public class ChangeInfoActivity extends BaseSecondActivity {
    @BindView(R.id.changeinfo_head)
    ExpandHeadView mChangeinfoHead;
    @BindView(R.id.changewhat)
    EditText mChangeWhat;
    public String changeWhat;

    @Override
    protected int setContentView() {
        return R.layout.activity_changeinfo;
    }

    @Override
    protected ExpandHeadView setExpandHeadView() {
        return setHead();
    }

    private ExpandHeadView setHead() {
        changeWhat = getIntent().getStringExtra("changeWhat");
        mChangeinfoHead.getHeadViewMidText().setText("修改" + changeWhat);
        return mChangeinfoHead;
    }

    @Override
    protected void initView() {
        mChangeinfoHead.getHeadViewRightText().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                uploadInfoToServer(changeWhat);
            }
        });
    }

    /**
     * 上传信息给服务器
     */
    private void uploadInfoToServer(String changeWhat) {
        if (TextUtils.equals(changeWhat, "昵称")) {
            changeWhat = "cbContacts";
        } else if (TextUtils.equals(changeWhat, "手机号码")) {
            changeWhat = "cbContphone";
        } else if (TextUtils.equals(changeWhat, "公司名称")) {
            changeWhat = "cbName";
        }
        String changeWhatValue = mChangeWhat.getText().toString().trim();
        String busnissid = SPUtils.getString("businessid", "");
        ALog.d(busnissid);
        OkHttpUtils.post().url(Constants.CHANGECARBUSINESS_URL)
                .addParams("sign", Constants.SIGN)
                .addParams("appFlag", "true")
                .addParams("id", busnissid)
                .addParams(changeWhat, changeWhatValue)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                MyToast.show("网络开了个小差");
            }

            @Override
            public void onResponse(String response, int id) {
                ALog.d(response);
                MsgBean msgBean = new Gson().fromJson(response, MsgBean.class);
                if (msgBean.getSuccess() == 0) {
                    MyToast.show("保存成功");
                    finish();
                } else {
                    MyToast.show(msgBean.getMessage());
                    return;
                }
            }
        });
    }


}
